package com.example.fuluchou.controller;

import com.example.fuluchou.common.errorcode.ControllerErrorCodeConstants;
import com.example.fuluchou.common.exception.ControllerException;
import com.example.fuluchou.common.pojo.CommonResult;
import com.example.fuluchou.common.untils.JacksonUtil;
import com.example.fuluchou.controller.param.CreateActivityParam;
import com.example.fuluchou.controller.param.PageParam;
import com.example.fuluchou.controller.result.ActivityDetailResult;
import com.example.fuluchou.controller.result.CreateActivityResult;
import com.example.fuluchou.controller.result.FindActivityListResult;
import com.example.fuluchou.service.ActivityService;
import com.example.fuluchou.service.dto.ActivityDTO;
import com.example.fuluchou.service.dto.ActivityDetailDTO;
import com.example.fuluchou.service.dto.CreateActivityDTO;
import com.example.fuluchou.service.dto.PageListDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.stream.Collectors;

@RestController
public class ActivityController {

    @Autowired
    ActivityService activityService;

    private static final Logger logger = LoggerFactory.getLogger(ActivityController.class);

    /**
     * 创建活动
     * @param createActivityParam
     * @return
     */
    @RequestMapping("/activity/create")
    public CommonResult<CreateActivityResult> createActivity(
            @Validated @RequestBody CreateActivityParam createActivityParam){
        //prizeId 3   4    userId  8   10
        logger.info("CreateActivityParam createActivityParam:{}", JacksonUtil.writeValueAsString(createActivityParam));
        return CommonResult.success(converToCreateActivityResult(activityService.createActivity(createActivityParam)));
    }

    /**
     * 查询活动详细信息：奖品列表，人员列表
     */
    @RequestMapping("/activity-detail/find")
    public CommonResult<ActivityDetailResult> findActivityDetail(
            @RequestParam(value = "activityId",required = false) Long activityId){
        logger.info("getActivity activityId: " + activityId);
        ActivityDetailDTO activityDetailDTO = activityService.getActivityDetail(activityId);
        return CommonResult.success(converToActivityDetailResult(activityDetailDTO));
    }

    private ActivityDetailResult converToActivityDetailResult(ActivityDetailDTO activityDetailDTO) {
        ActivityDetailResult result = new ActivityDetailResult();
        result.setActivityId(activityDetailDTO.getActivityId());
        result.setActivityName(activityDetailDTO.getActivityName());
        result.setDescription(activityDetailDTO.getDescription());
        result.setValid(activityDetailDTO.valid());
        result.setPrizes(activityDetailDTO.getPrizeDOList()
                .stream()
                .map(prizeDTO -> {
                    ActivityDetailResult.Prize prize = new ActivityDetailResult.Prize();
                    prize.setPrizeId(prizeDTO.getPrizeId());
                    prize.setName(prizeDTO.getName());
                    prize.setDescription(prizeDTO.getDescription());
                    prize.setPrice(prizeDTO.getPrice());
                    prize.setImageUrl(prizeDTO.getImageurl());
                    prize.setStatus(prizeDTO.getStatus());
                    prize.setPrizeTierName(prizeDTO.getTiers());
                    prize.setPrizeAmount(prizeDTO.getAmount());
                    prize.setValid(prizeDTO.valid());
                    return prize;
                })
                .collect(Collectors.toList()));
        result.setUsers(activityDetailDTO.getUserDOList()
                .stream()
                .map(userDTO->{
                    ActivityDetailResult.User user = new ActivityDetailResult.User();
                    user.setUserId(userDTO.getUserId());
                    user.setUserName(userDTO.getUserName());
                    user.setStatus(userDTO.getStatus());
                    user.setValid(userDTO.valid());
                    return user;
                })
                .collect(Collectors.toList()));
        return result;
    }

    /**
     * 获取活动列表
     * @param param
     * @return
     */
    @RequestMapping("/activity/find-list")
    public CommonResult<FindActivityListResult> findActivityList(
            PageParam param){
        logger.info("findActivityList findActivityListParam:{}",
                JacksonUtil.writeValueAsString(param));
        return CommonResult.success( converToFindActivityListResult(
                activityService.findActivityList(param)));
    }

    private FindActivityListResult converToFindActivityListResult(PageListDTO<ActivityDTO> activityList) {
        if(null == activityList){
            throw new ControllerException(ControllerErrorCodeConstants.ACTIVITIES_FIND_ERROR);
        }
        FindActivityListResult result = new FindActivityListResult();
        result.setTotal(activityList.getTotal());
        result.setRecords(activityList.getRecords()
                .stream()
                .map(activityDTO->{
                            FindActivityListResult.ActivityInfo activityInfo = new FindActivityListResult.ActivityInfo();
                            activityInfo.setActivityId(activityDTO.getActivityId());
                            activityInfo.setActivityName(activityDTO.getActivityName());
                            activityInfo.setDescription(activityDTO.getDescription());
                            activityInfo.setValid(activityDTO.valid());
                            return activityInfo;
                        }
                )
                .collect(Collectors.toList()));
        return result;

    }


    private CreateActivityResult converToCreateActivityResult(CreateActivityDTO activityId) {
        CreateActivityResult result = new CreateActivityResult();
        result.setActivityId(activityId.getActivityId());
        return result;
    }

}
